package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
类简介：

功能：

时间：2020/3/30

*/
@Controller
@RequestMapping("/checkGroup")
@ResponseBody
public class CheckGroupController {

    @Reference
    private CheckItemService checkItemService;

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/findCheckitems")
    private Result findCheckItems(){
        List<CheckItem> checkItems;
        try {
            checkItems = checkItemService.findAll();
        }catch (Exception e){
            return new Result(false, e.getMessage());
        }
        return new Result(true,"成功",checkItems);
    }

    @RequestMapping("/add")
    private Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL,e.getMessage());
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.findPage(queryPageBean);
    }

    @RequestMapping("findById")
    public Result findById(int id){
        CheckGroup checkGroup;
        try {
            checkGroup = checkGroupService.findById(id);
        }catch (Exception e){
            return new Result(false,"获取最新信息失败",e.getMessage());
        }
        return new Result(true,"获取最新信息成功",checkGroup);

    }

    @RequestMapping("/updata")
    public Result updata(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        try {
            checkGroupService.upfata(checkGroup,checkitemIds);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL,e.getMessage());
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);

    }

    @RequestMapping("findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups;
        try {
            checkGroups = checkGroupService.findAll();
        }catch (Exception e){
            return new Result(false,"获取信息失败",e.getMessage());
        }
        return new Result(true,"获取最新信息成功",checkGroups);

    }
}
